//
//  GMArtworkListCell.m
//  gentleman
//
//  Created by 范月成 on 2017/12/5.
//  Copyright © 2017年 fancy. All rights reserved.
//

#import "GMArtworkListCell.h"
#import "GMUIHelper.h"
#import "VendorHeader.h"

@interface GMArtworkListCell ()

@property (nonatomic, strong) UIImageView *thumbView;
@property (nonatomic, strong) YYLabel *titleLabel;
@property (nonatomic, strong) YYLabel *uploaderLabel;
@property (nonatomic, strong) YYLabel *catgeoryLabel;
@property (nonatomic, strong) YYLabel *langueLabel;
@property (nonatomic, strong) YYLabel *timeLabel;
@property (nonatomic, strong) YYLabel *countLabel;
@property (nonatomic, strong) YYLabel *starLabel;

@end

@implementation GMArtworkListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self addSubviews];
    }
    return self;
}

- (void)addSubviews {
    [self.contentView addSubview:self.thumbView];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.uploaderLabel];
    [self.contentView addSubview:self.catgeoryLabel];
    [self.contentView addSubview:self.starLabel];
    [self.contentView addSubview:self.timeLabel];
    [self.contentView addSubview:self.countLabel];
    [self.contentView addSubview:self.langueLabel];
}

- (void)setArtwork:(GMArtwork *)artwork {
    _artwork = artwork;
    [self.thumbView sd_setImageWithURL:[NSURL URLWithString:artwork.thumb]
                      placeholderImage:[UIImage placeholderImage]];
    self.titleLabel.text = artwork.title;
    self.uploaderLabel.text = artwork.uploader;
    self.starLabel.text = [NSString stringWithFormat:@"评分:%.1f 星", artwork.rating];
    self.catgeoryLabel.text = [NSString stringWithFormat:@" %@ ", artwork.category];
    self.catgeoryLabel.backgroundColor = [UIColor colorWithCategory:artwork.category];
    self.timeLabel.text = [NSString timeIntervalToTimeString:artwork.posted];
    self.countLabel.text = [NSString stringWithFormat:@"%ld 页", artwork.filecount];
    self.langueLabel.text = [artwork getArtworkLanguage];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.thumbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(6);
        make.bottom.equalTo(self.contentView).offset(-6);
        make.left.equalTo(self.contentView).offset(12);
        make.width.mas_equalTo(self.contentView.mas_height).multipliedBy(0.618);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(6);
        make.left.equalTo(self.thumbView.mas_right).offset(6);
        make.right.equalTo(self.contentView).offset(-12);
    }];
    
    [self.uploaderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(6);
        make.left.equalTo(self.thumbView.mas_right).offset(6);
    }];
    
    [self.starLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(6);
        make.right.equalTo(self.contentView.mas_right).offset(-6);
    }];
    
    [self.catgeoryLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.uploaderLabel.mas_bottom).offset(6);
        make.left.equalTo(self.thumbView.mas_right).offset(6);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.thumbView);
        make.left.equalTo(self.thumbView.mas_right).offset(6);
    }];
    
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.thumbView);
        make.right.equalTo(self.contentView.mas_right).offset(-12);
    }];
    
    [self.langueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.countLabel.mas_top).offset(-6);
        make.right.equalTo(self.contentView.mas_right).offset(-12);
    }];
}

#pragma mark - getter
- (UIImageView *)thumbView {
    if (!_thumbView) {
        _thumbView = [[UIImageView alloc] init];
        _thumbView.contentMode = UIViewContentModeScaleAspectFill;
        _thumbView.clipsToBounds = YES;
    }
    return _thumbView;
}

- (YYLabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [GMUIHelper generalTitleLabel];
        _titleLabel.numberOfLines = 2;
    }
    return _titleLabel;
}

- (YYLabel *)uploaderLabel {
    if (!_uploaderLabel) {
        _uploaderLabel = [GMUIHelper generalSubTitleLabel];
    }
    return _uploaderLabel;
}

- (YYLabel *)starLabel {
    if (!_starLabel) {
        _starLabel = [GMUIHelper generalSubTitleLabel];
    }
    return _starLabel;
}

- (YYLabel *)catgeoryLabel {
    if (!_catgeoryLabel) {
        _catgeoryLabel = [GMUIHelper labelWithFont:kSystemFont(14) textColor:[UIColor whiteColor]];
    }
    return _catgeoryLabel;
}

- (YYLabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [GMUIHelper generalSubTitleLabel];
    }
    return _timeLabel;
}

- (YYLabel *)langueLabel {
    if (!_langueLabel) {
        _langueLabel = [GMUIHelper generalSubTitleLabel];
    }
    return _langueLabel;
}

- (YYLabel *)countLabel {
    if (!_countLabel) {
        _countLabel = [GMUIHelper generalSubTitleLabel];
    }
    return _countLabel;
}

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (NSArray<GMSkeletonView *> *)skeletonLayout {
    CGFloat imgW = 44*3*0.618;
    
    
    GMSkeletonView *v1 = [[GMSkeletonView alloc] initWithFrame:CGRectMake(12, 6, imgW, 44*3-12)];
    GMSkeletonView *v2 = [[GMSkeletonView alloc] initWithFrame:CGRectMake(12+imgW+6, 6, kScreenWidth-24-imgW-6, 15)];
    GMSkeletonView *v3 = [[GMSkeletonView alloc] initWithFrame:CGRectMake(12+imgW+6, 27, 100, 15)];
    GMSkeletonView *v4 = [[GMSkeletonView alloc] initWithFrame:CGRectMake(kScreenWidth-110, 27, 100, 15)];
    GMSkeletonView *v5 = [[GMSkeletonView alloc] initWithFrame:CGRectMake(12+imgW+6, 48, 100, 20)];
    GMSkeletonView *v6 = [[GMSkeletonView alloc] initWithFrame:CGRectMake(12+imgW+6, 44*3-21, 70, 15)];
    GMSkeletonView *v7 = [[GMSkeletonView alloc] initWithFrame:CGRectMake(kScreenWidth-85, 44*3-21, 70, 15)];
 
    return @[v1, v2, v3, v4, v5, v6, v7];
}


@end
